# -*- coding: utf-8 -*-
# created on 2016/11/30

from mathsolver.functions.base import *


# 更新公比值
class GongBiValueUpdate001(BaseFunction):
    """
    eg. 公比为\\sqrt[3]{2}的等比数列{a_{n}}的各项都是正数,a_{3}a_{11}=16,则\\log_{2}a_{16}= ()
    """
    def solver(self, *args):
        assert len(args) == 2
        assert isinstance(args[0], BaseSequence)
        assert isinstance(args[1], (BasePoly, BaseNumber, BaseVariable))
        sl = self.search(args[0].name)
        value = args[1].sympify()
        sl.qValue = value
        self.output.append(sl)
        return self


class GongBiValueUpdate002(BaseFunction):
    """
    eg. 首项为1,公比为2的等比数列的前4项和S_{4}=.
    """
    def solver(self, *args):
        assert len(args) == 2
        assert isinstance(args[0], BaseSeqPoly)
        p = r'[a-zA-Z]_\(.*?\)'
        m = re.findall(p, args[0].value)
        m = set(m)
        m = list(m)
        assert len(m) == 1
        m = m[0]
        m = sympify(m)
        m_name = m.func
        sl = self.search(str(m_name))
        assert isinstance(args[1], (BaseNumber, BaseVariable))
        value = args[1].sympify()
        sl.qValue = value
        self.output.append(sl)
        return self


class GongBiValueUpdate003(BaseFunction):
    """
    已知等比数列的公比为2,且前5项和为1,则前10项的和等于(   )
    """
    def solver(self, *args):
        assert len(args) == 1
        assert isinstance(args[0], (BaseVariable, BaseNumber, BasePoly))
        sls = [self.known[item] for item in self.known if isinstance(self.known[item], BaseShuLieDengBi)]
        assert len(sls) == 1
        sl = sls[0]
        sl.qValue = args[0].sympify()
        return self


class GongBiValueUpdate004(BaseFunction):
    """
    已知等比数列{a_{n}}的公比为q=-\\frac{1}{2}.若a_{3}=\\frac{1}{4},求数列{a_{n}}的前n项和.
    """
    def solver(self, *args):
        assert len(args) == 2
        assert isinstance(args[0], BaseSequence)
        assert isinstance(args[1], (BaseEq, BaseSeqEq))
        sl = self.search(args[0].name)
        eq = args[1].sympify()
        q_value = eq[1]
        sl.qValue = q_value
        return self


class GongBiValueUpdate005(BaseFunction):
    """
    已知等比数列{a_{n}}中,a_{1}=\\frac{1}{3},公比q=\\frac{1}{3}.S_{n}为{a_{n}}的前n项和,证明:S_{n}=\\frac{1-a_{n}}{2}
    """
    def solver(self, *args):
        assert len(args) == 1
        assert isinstance(args[0], (BaseEq, BaseSeqEq))
        sls = [self.known[item] for item in self.known if isinstance(self.known[item], BaseSequence)]
        assert len(sls) == 1
        sl = sls[0]
        eq = args[0].sympify()
        q_value = eq[1]
        sl.qValue = q_value
        return self


class GongBiValueUpdate(BaseFunction):
    CLS = [GongBiValueUpdate001, GongBiValueUpdate002, GongBiValueUpdate003, GongBiValueUpdate004, GongBiValueUpdate005]

    def solver(self, *args):
        r = None
        for cl in GongBiValueUpdate.CLS:
            try:
                r = cl(known=self.known, verbose=True).solver(*args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r
